package org.budo.cmbc.sdk.entity;

import lombok.extern.slf4j.Slf4j;
import org.budo.xml.XmlObject;

/**
 * @author lmw
 */
@Slf4j
public class QryBatchCostReimbResponse extends AbstractCmbcResponse {
    private static final long serialVersionUID = 6420684619939019226L;

    public QryBatchCostReimbResponse(String responseXml) {
        this.setResponseBody(responseXml);
    }

    public String getFileContent() {
        XmlObject xDataBody = this.xDataBody(this.getResponseBody());

        if (null == xDataBody) {
            log.error("#22 xDataBody=" + xDataBody + ", responseBody=" + this.getResponseBody());
            return null;
        }

        XmlObject batchTransfers = xDataBody.getChildNodeByName("batchTransfers");
        if (null == batchTransfers) {
            log.error("#28 xDataBody=" + xDataBody + ", responseBody=" + this.getResponseBody());
            return null;
        }

        return batchTransfers.getChildNodeTextContentByName("fileContent");
    }

    public String getStatusErrMsg() {
        XmlObject xDataBody = this.xDataBody(this.getResponseBody());

        if (null == xDataBody) {
            log.error("#38 xDataBody=" + xDataBody + ", responseBody=" + this.getResponseBody());
            return null;
        }

        XmlObject statusId = xDataBody.getChildNodeByName("statusId");
        if (null == statusId) {
            log.error("#44 xDataBody=" + xDataBody + ", responseBody=" + this.getResponseBody());
            return null;
        }

        return statusId.getChildNodeTextContentByName("statusErrMsg");
    }

    @Override
    public String toString() {
        if ("对账接口报错：文件正在处理中".equals(this.getMessage())) {
            return super.toString() + ", " + this.getMessage();
        }

        if ("ok".equals(this.getMessage())) {
            return super.toString() + ", " + this.getStatusErrMsg();
        }

        return super.toString() + ", responseBody=" + this.getResponseBody();
    }
}

//<?xml version="1.0" encoding="UTF-8"?>
//<CMBC trnCode="qryBatchCostReimb" security="none" lang="chs" header="100" version="100">
//<responseHeader>
//  <status>
//    <code>0</code>
//    <severity>info</severity>
//    <message>ok</message>
//  </status>
//  <dtServer>2021-04-16 15:27:37</dtServer>
//  <userKey>N</userKey>
//  <dtDead/>
//  <language>chs</language>
//</responseHeader>
//<xDataBody>
//  <statusId>
//    <statusCode>0</statusCode>
//    <statusSeverity>ok</statusSeverity>
//    <statusErrMsg>批量支付受理成功！</statusErrMsg>
//  </statusId>
//  <batchTransfers>
//    <trnId>TJ21416152242215</trnId>
//    <insId>210416152242215</insId>
//    <fileContent>6222822308006483833|刘海建|工资奖金淘金-筹控-报酬|0.01|20|调用主机失败:输入[19]位帐号[6222822308006483833]不存在|uajy3vav|^</fileContent>
//  </batchTransfers>
//</xDataBody>
//</CMBC>

//<?xml version="1.0" encoding="UTF-8"?>
//
//<CMBC trnCode="qryBatchCostReimb" security="none" lang="chs" header="100" version="100">
//  <responseHeader>
//    <status>
//      <code>0</code>
//      <severity>info</severity>
//      <message>ok</message>
//    </status>
//    <dtServer>2021-04-16 17:24:05</dtServer>
//    <userKey>N</userKey>
//    <dtDead/>
//    <language>chs</language>
//  </responseHeader>
//  <xDataBody>
//    <statusId>
//      <statusCode>0</statusCode>
//      <statusSeverity>ok</statusSeverity>
//      <statusErrMsg>批量支付受理成功！</statusErrMsg>
//    </statusId>
//    <batchTransfers>
//      <trnId>TJ21416171503041</trnId>
//      <insId>210416171503041</insId>
//      <fileContent>6226223300914812|大善人11|工资奖金淘金-筹控-报酬|0.04|10|success|tj123456|^</fileContent>
//    </batchTransfers>
//  </xDataBody>
//</CMBC>
